<template>
  <q-page class="q-pa-md">



    <div class="q-pa-md">
      <q-card>
        <q-card-section class="row items-center">
          <div class="text-subtitle1">1、预案信息</div>
        </q-card-section>

        <q-card-section class="row q-gutter-sm">
          <q-btn color="primary" label="线路名称" style="width: 20%"></q-btn>
          <q-btn color="white" text-color="black" :label="xianlu" style="width: 20%"></q-btn>

        </q-card-section>

        <q-card-section>
          <q-btn color="primary" label="预案内容" style="width: 30%"></q-btn>
<!--          预案列表         -->
          <div class="q-pa-md">
            <q-table

              :data="data"
              :columns="columns"
              row-key="name"
            />
          </div>

        </q-card-section>
      </q-card>
    </div>



    <div class="q-pa-md" >
      <q-card>
        <q-card-section class="row items-center">
          <div class="text-subtitle1">2、线路最大负荷</div>
        </q-card-section>

        <q-card-section class="row q-gutter-sm">
          <q-btn color="primary" label="发生时间" style="width: 20%"></q-btn>
          <q-btn color="white" text-color="black" :label="shijian" style="width: 20%"></q-btn>
          <q-btn color="primary" label="最大负荷" style="width: 20%"></q-btn>
          <q-btn color="white" text-color="black" :label="fuhe" style="width: 20%"></q-btn>
        </q-card-section>

        <q-card-section>
          <q-btn block color="primary" label="线路名称"></q-btn>
          <q-btn color="white" text-color="black" :label="xianlu" style="width: 30%"></q-btn>
        </q-card-section>
      </q-card>
    </div>


    <div class="q-pa-md" >
      <q-card>
        <q-card-section class="row items-center">
          <div class="text-subtitle1">3、定值单状态</div>
        </q-card-section>

        <q-card-section class="row q-gutter-sm">
          <q-btn color="primary" label="线路名称" style="width: 20%"></q-btn>
          <q-btn color="white" text-color="black" :label="xianlu" style="width: 20%"></q-btn>

        </q-card-section>

        <q-card-section>
          <div class="q-pa-md">
            <q-table
              :data="dingzhirows"
              :columns="dingzhicolumns"
              row-key="id"
            />
          </div>
        </q-card-section>
      </q-card>
    </div>




    <div class="q-pa-md">
      <q-card>
        <q-card-section class="row items-center">
          <div class="text-subtitle1">4、操作票检索结果</div>
        </q-card-section>

        <q-card-section class="row q-gutter-sm">
          <q-btn color="primary" label="线路名称" style="width: 20%"></q-btn>
          <q-btn color="white" text-color="black" :label="xianlu" style="width: 20%"></q-btn>

        </q-card-section>

        <q-card-section>
          <q-btn color="primary" label="操作票类" ></q-btn>
          <q-btn color="white" text-color="black" :label="caozuopiao" ></q-btn>
        </q-card-section>

        <q-card-section>
          <div class="q-pa-md">
            <q-table
              :data="caozuopiaorows"
              :columns="caozuopiaocolumns"
              row-key="id"
            />
          </div>
        </q-card-section>

      </q-card>
    </div>



  </q-page>
</template>

<script>
import * as XLSX from 'xlsx';
import axios from 'axios';
export default {
  data() {
    return {
      xianlu:'线路名称',
      shijian:'发生时间',
      fuhe:'最大负荷',
      number: '',
      label: '',
      // 推荐生成
      sqlgenPro:'',

      //
      queryString:'',
      showSelects3:false,

      showSelects1: false,

      showSelects2:false,
      // 预案
      ywNamescomps:'',
      ywNamesnamedetail:'',
      ywNamescodename:'',
      ywNamescodevalue:'',



      queryInputcomps:'',
      queryInputnamedetail:'',
      queryInputcodename:'',
      queryInputcodevalue:'',

      filteredOptionscomps:[],
      filteredOptionsnamedetail:[],
      filteredOptionscodename:[],
      filteredOptionscodevalue:[],

      // 操作票


      ywNamessysunitname:'',
      ywNamessyscontent:'',
      ywNamescomunitname:'',
      ywNamescomcontent:'',


      queryInputsysunitname:'',
      queryInputsyscontent:'',
      queryInputcomunitname:'',
      queryInputcomcontent:'',

      filteredOptionssysunitname:[],
      filteredOptionssyscontent:[],
      filteredOptionscomunitname:[],
      filteredOptionscomcontent:[],


      // 》》》》》》》》》》》》
      searchQuery:'',
      columns2:[],
      selectedColumns2: [], // 用户选择显示的列名数组
      filteredOptions2: [],  // 过滤后的选项
      ywNames2:'',
      queryInput2: '',
      queries2: [],
      result2: '',
      querySearchResult2:[],
      caozuopiao:'送电逐项令、停电逐项令、综合令',
      dingzhirows: [
        { id: 1, name: '参数1', value: '值1' },
        { id: 2, name: '参数2', value: '值2' },
        { id: 3, name: '参数3', value: '值3' }
      ],
      // 列定义，包括定值名称和定值
      dingzhicolumns: [
        { name: 'name', required: true, label: '定值名称', field: 'name', sortable: true },
        { name: 'value', label: '定值', field: 'value', sortable: true }
      ],


      caozuopiaorows: [
        { id: 1, name: 1, value: '值1' },
        { id: 2, name: 2, value: '值2' },
        { id: 3, name: 3, value: '值3' }
      ],
      // 列定义，包括定值名称和定值
      caozuopiaocolumns: [
        { name: 'name', required: true, label: '顺序', field: 'name', sortable: true },
        { name: 'value', label: '操作项目', field: 'value', sortable: true }
      ],


      columns: [
        { name: 'index', label: '序号', field: 'index', align: 'left', sortable: true },
        { name: 'description', label: '预案内容', field: 'description', align: 'left', sortable: true },
      ],
      data: [
        { index: 1, description: '项目1' },
        { index: 2, description: '项目2' },
        { index: 3, description: '项目3' }
      ],
      showSelects: false, // 控制 q-select 显示的状态
      selectedColumns: [], // 用户选择显示的列名数组
      filteredOptions: [],  // 过滤后的选项
      ywNames:'',
      queryInput: '',
      queries: [],
      result: '',
      querySearchResult:[],
    };
  },
  computed:{
    filteredColumns() {
      return this.selectedColumns;
    },

  },
  methods: {
    populateTableData() {
      // this.data = this.ywNamesnamedetail.map((detail, index) => ({
      //   id: index + 1,  // 从 1 开始递增的 id
      //   index: index + 1,  // 从 1 开始递增的 id
      //   align: 'left',
      //   description: detail // 每一项的详情作为 description
      // }));
    },
    generateLabel() {
      // 模拟标签生成
      this.label = '新标签' + Math.random().toString(36).substring(7);
    },
    saveData() {
      // 保存数据逻辑
      alert('数据已保存');
    },
    searchData(){},


    // 预案
    filterOptionscomps(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionscomps = this.ywNamescomps.filter(v => v.match(filter))
        } else {
          this.filteredOptionscomps = this.ywNamescomps
        }
      });
    },


    filterOptionsnamedetail(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionsnamedetail = this.ywNamesnamedetail.filter(v => v.match(filter))
        } else {
          this.filteredOptionsnamedetail = this.ywNamesnamedetail
        }
      });
    },

    filterOptionscodename(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionscodename = this.ywNamescodename.filter(v => v.match(filter))
        } else {
          this.filteredOptionscodename = this.ywNamescodename
        }
      });
    },

    filterOptionscodevalue(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionscodevalue = this.ywNamescodevalue.filter(v => v.match(filter))
        } else {
          this.filteredOptionscodevalue = this.ywNamescodevalue
        }
      });
    },

    //操作票
    filterOptionssysunitname(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionssysunitname = this.ywNamessysunitname.filter(v => v.match(filter))
        } else {
          this.filteredOptionssysunitname = this.ywNamessysunitname
        }
      });
    },
    filterOptionssyscontent(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionssyscontent = this.ywNamessyscontent.filter(v => v.match(filter))
        } else {
          this.filteredOptionssyscontent = this.ywNamessyscontent
        }
      });
    },
    filterOptionscomunitname(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionscomunitname = this.ywNamescomunitname.filter(v => v.match(filter))
        } else {
          this.filteredOptionscomunitname = this.ywNamescomunitname
        }
      });
    },
    filterOptionscomcontent(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptionscomcontent = this.ywNamescomcontent.filter(v => v.match(filter))
        } else {
          this.filteredOptionscomcontent = this.ywNamescomcontent
        }
      });
    },

    // 》》》》》》》》》


    filterOptions(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptions = this.ywNames.filter(v => v.match(filter))
        } else {
          this.filteredOptions = this.ywNames
        }
      });
    },
    filterOptions2(val, update) {
      update(() => {
        if (val) {
          const filter = new RegExp(val, 'i')
          this.filteredOptions2 = this.ywNames2.filter(v => v.match(filter))
        } else {
          this.filteredOptions2 = this.ywNames2
        }
      });
    },
    addQuery() {
      this.search();
      // if (this.queryInput) {
      //   this.queries.push(this.queryInput);
      //
      //   this.search();
      //   // this.queryInput = '';
      // }
    },
    removeQuery(index) {

      this.queries.splice(index, 1);
      this.search();
    },
    searchQuery_native(){
      console.log("------------------")
    },
    buildElasticsearchQuery(queryInput, operatemission,sysunitname,syscontent,comunitname,comcontent,comps,namedetail,codename,codevalue) {
      const mustClauses = [];
      var queryString = ''
      // 始终检查'linename'字段
      if (queryInput) {
        mustClauses.push({
          "match_phrase": {
            "linename": queryInput
          }
        });


        queryString = queryString + "linename" + " = '" + queryInput + "' and "

      }


      // 如果operatemission有值，也检查'description'字段
      if (operatemission) {
        mustClauses.push({
          "match_phrase": {
            "operatemission": operatemission
          }
        });

        queryString = queryString + " operatemission" + " = '" + operatemission + "' and "

      }

      // 操作票


      if (sysunitname) {
        mustClauses.push({
          "match_phrase": {
            "sysunitname": sysunitname
          }
        });

        queryString = queryString + " sysunitname" + " = '" + sysunitname + "' and "

      }

      if (syscontent) {
        mustClauses.push({
          "match_phrase": {
            "syscontent": syscontent
          }
        });


        queryString = queryString + " syscontent" + " = '" + syscontent + "' and "

      }

      if (comunitname) {
        mustClauses.push({
          "match_phrase": {
            "comunitname": comunitname
          }
        });

        queryString = queryString + " comunitname" + " = '" + comunitname + "' and "

      }

      if (comcontent) {
        mustClauses.push({
          "match_phrase": {
            "comcontent": comcontent
          }
        });


        queryString = queryString + " comcontent" + " = '" + comcontent + "' and "

      }

// 预案
      if (comps) {
        mustClauses.push({
          "match_phrase": {
            "comps": comps
          }
        });

        queryString = queryString + " comps" + " = '" + comps + "' and "

      }

      if (namedetail) {
        mustClauses.push({
          "match_phrase": {
            "namedetail": namedetail
          }
        });

        queryString = queryString + " namedetail" + " = '" + namedetail + "' and "

      }

      if (codename) {
        mustClauses.push({
          "match_phrase": {
            "codename": codename
          }
        });

        queryString = queryString + " codename" + " = '" + codename + "' and "

      }

      if (codevalue.toString()) {
        mustClauses.push({
          "match_phrase": {
            "codevalue": codevalue.toString()
          }
        });

        queryString = queryString + " codevalue" + " = '" + codevalue + "' and "


      }

      this.queryString = queryString




      // 构建查询JSON
      const query = {
        "query": {
          "bool": {
            "must": mustClauses
          }
        }
      };

      return query;
    },

    exportToExcel() {
          const data = this.querySearchResult.map(row => {
            const rowData = {};
            this.filteredColumns.forEach(column => {
              rowData[column.label] = row[column.field];
            });
            return rowData;
          });

          const ws = XLSX.utils.json_to_sheet(data);
          const wb = XLSX.utils.book_new();
          XLSX.utils.book_append_sheet(wb, ws, "Results");
          XLSX.writeFile(wb, "export.xlsx");
        },
    async search() {
      console.log("========search=======")
      var queryString  = this.buildElasticsearchQuery(this.queryInput, this.queryInput2,this.queryInputsysunitname,this.queryInputsyscontent,this.queryInputcomunitname,this.queryInputcomcontent,this.queryInputcomps,this.queryInputnamedetail,this.queryInputcodename,this.queryInputcodevalue);
      console.log(queryString)

      console.log(this.queryString+" 1=1")

      var sqlgen = "select * from sqlgen where " +  this.queryString+" 1=1"
      console.log(sqlgen)

      this.sqlgenPro = sqlgen
      // 发送查询请求
      const response = await axios.post(
        'http://43.136.220.136:9002/sqlgen/_search'
        ,queryString,
        {
          headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Basic ' + btoa('admin:1992A1573')
          }
        }
      );
      console.log("-----------------------")
      console.log(response)

      this.searchResults = response.data.hits.hits.map(hit => hit._source);
      // 处理响应数据
      if (this.searchResults.length > 0) {
        this.columns = Object.keys(this.searchResults[0]).map(key => ({
          name: key,
          label: key, // 可以在这里将 key 转换为更友好的标签
          field: key,
          align: 'left',
          sortable: true
        }));
      }
      this.querySearchResult = this.searchResults

      // data.push({"key":hits[0].key+"-"+hits2[0].key,"value":hits[0].doc_count})

    //   try {
    //     // 发送 GET 请求并等待响应
    //     // 将条件传过去就ok了 当前输入数组内容作为参数传入。
    //     console.log(this.queries)
    //     const response = await axios.get('http://127.0.0.1:4444/get_xiangtongyongtu_names', {
    //     params: { items_yw: JSON.stringify(this.queries) }  // 确保键名与后端匹配
    //   });
    //     // 处理成功情况
    //
    //     console.log('Road names:', response.data.road_names);
    //     this.result = `当前查询结果: ${JSON.stringify({"所属光缆":response.data[0]['所属光缆'],"统计值":response.data[0]['数量']})}`;
    //     localStorage.setItem('渲染',JSON.stringify(response.data[0]))
    //
    //     return ;
    // } catch (error) {
    //     // 处理错误情况
    //     console.error('Error fetching data:', error);
    // }

      // console.log("search")
      // const uniqueItems = {};
      // const data = [];
      // var queriesq = []
      // console.log(this.queries)
      //
      // for (const query1 of this.queries) {
      //   queriesq = []
      //   queriesq.push(query1)
      //     console.log(query1)
      //     if (queriesq.length>0) {
      //     if (!queriesq.includes("SDH")) {
      //           queriesq.push("SDH");
      //         }
      //     // 构建查询条件
      //     const mustQueries = queriesq.map(condition => ({
      //       match_phrase: {
      //         "对应光路": condition
      //       }
      //     }));
      //
      //     // 构建最终查询
      //     const query = {
      //       bool: {
      //         must: mustQueries
      //       }
      //     };
      //
      //     try {
      //       // 发送查询请求
      //       const response = await axios.post(
      //         'http://43.136.220.136:9002/xianmen_guanglu/_search',
      //         {
      //           query: query,
      //          "size": 0,
      //           "aggs": {
      //             "by_belonging_cable": {
      //               "terms": {
      //                 "field": "所属光缆.keyword"
      //               },
      //               "aggs": {
      //                 "unique_routes": {
      //                   "cardinality": {
      //                     "field": "对应光路.keyword"
      //                   }
      //                 }
      //               }
      //             },
      //             "by_route": {
      //               "terms": {
      //                 "field": "对应光路.keyword"
      //               },
      //               "aggs": {
      //                 "unique_cables": {
      //                   "cardinality": {
      //                     "field": "所属光缆.keyword"
      //                   }
      //                 }
      //               }
      //             }
      //           }
      //         },
      //         {
      //           headers: {
      //             'Content-Type': 'application/json',
      //             'Authorization': 'Basic ' + btoa('admin:1992A1573')
      //           }
      //         }
      //       );
      //
      //       // 处理响应数据
      //       const hits = response.data.aggregations.by_belonging_cable.buckets;
      //       const hits2 = response.data.aggregations.by_route.buckets;
      //       const result = hits[0].key;
      //       data.push({"key":hits[0].key+"-"+hits2[0].key,"value":hits[0].doc_count})
      //
      //     } catch (error) {
      //       console.error("查询失败:", error);
      //       this.result = '查询失败';
      //     }
      //   } else {
      //     this.result = '暂无查询内容';
      //   }
      // }
      //
      //  this.result = `当前查询结果: ${JSON.stringify(data)}`;

    }
  },
  async mounted() {

    this.caozuopiao = localStorage.getItem('caozuopiao')
    this.shijian = localStorage.getItem('shijian')
    if(this.caozuopiao==='66千伏平凤二线送电'){
      this.ywNamessyscontent=['汇报66千伏平凤二线有关作业全结束，现场安全措施全拆除，人员全撤离，送电无问题。','汇报66千伏平凤二线及66千伏2号主变有关作业全结束，现场安全措施全拆除，人员全撤离，送电无问题。','拆除66千伏平凤二线10912线路侧地线。','拆除66千伏平凤二线4952线路侧地线。','合上66千伏平凤二线49522刀闸。','合上66千伏平凤二线49526刀闸。','合上66千伏平凤二线4952开关。','66千伏平凤二线10912开关及66千伏2号主变由检修转运行。','66千伏1号主变由运行转热备用。','投入主变互投装置。']
      // this.ywNamessyscontent=['汇报66千伏平凤二线有关作业全结束，现场安全措施全拆除，人员全撤离，送电无问题。','汇报66千伏平凤二线及66千伏2号主变有关作业全结束，现场安全措施全拆除，人员全撤离，送电无问题。','拆除66千伏平凤二线10912线路侧地线。','拆除66千伏平凤二线4952线路侧地线。','合上66千伏平凤二线49522刀闸。','合上66千伏平凤二线49526刀闸。','合上66千伏平凤二线4952开关。','66千伏平凤二线10912开关及66千伏2号主变由检修转运行。','66千伏1号主变由运行转热备用。','投入主变互投装置。']

    }else{
      this.ywNamessyscontent=['退出主变备自投装置。','66千伏1号主变由热备用转运行。','66千伏2号主变由运行转检修。','拉开66千伏平凤二线4952开关。','拉开66千伏平凤二线49526刀闸。','拉开66千伏平凤二线49522刀闸。','在66千伏平凤二线4952线路侧装设一组地线。','在66千伏平凤二线10912线路侧装设一组地线。','通知66千伏平凤二线及2号主变已停电，所内有关作业可以开始，现场安全措施自行负责。','通知67千伏平凤二线线路已停电，线路有关作业可以开始，现场安全措施自行负责。']
    }

    this.caozuopiaorows = this.ywNamessyscontent.map((content, index) => ({
      id: index + 1, // id 递增
      name: `${index + 1}`, // 格式化 name 字段为 "参数1", "参数2", 等
      value: content // 将字符串数组的每个元素设置为 value
    }));


    // 如果是这种，直接变更过去就好了。



    // localStorage.setItem('queryResult',JSON.stringify(sourceArray))
    var sourceArray = JSON.parse(localStorage.getItem('queryResult'));

//     const newArray = sourceArray.map((item, index) => {
//       return {
//         id: index + 1,  // 将id设置为递增序号，从1开始
//         name: item.codename,  // 新数组的name属性对应sourceArray的codename
//         value: item.codevalue  // 新数组的value属性对应sourceArray的codevalue
//       };
//     });
//
// // 现在newArray是一个新的数组，包含了重新定义的对象
//     console.log(newArray);

    // localStorage.setItem('queryInputcodevalue',this.queryInputcodevalue)
    const queryInputcodevalue =localStorage.getItem('queryInputcodevalue')
    // 如果queryInputcodevalue === '1' 就过滤value为1 的否则过滤value为0 的
    console.log("-=-=-==")
    console.log(queryInputcodevalue)

    var newArray =[{'id': 1, 'name': 'A通道差动保护', 'value': 0}, {'id': 11, 'name': 'B通道差动保护', 'value': 0}, {'id': 21, 'name': 'CT断线闭锁差动', 'value': 1}, {'id': 31, 'name': 'A通道通信内时钟', 'value': 1}, {'id': 41, 'name': 'B通道通信内时钟', 'value': 1}, {'id': 51, 'name': '振荡闭锁元件', 'value': 0}, {'id': 61, 'name': '距离保护Ⅰ段', 'value': 1}, {'id': 71, 'name': '距离保护Ⅱ段', 'value': 1}, {'id': 81, 'name': '距离保护Ⅲ段', 'value': 1}, {'id': 91, 'name': '投过流Ⅰ段', 'value': 0}, {'id': 101, 'name': '投过流Ⅱ段', 'value': 1}, {'id': 111, 'name': '投过流Ⅲ段', 'value': 1}, {'id': 121, 'name': '投过流Ⅳ段', 'value': 1}, {'id': 131, 'name': '过流Ⅰ段经方向', 'value': 0}, {'id': 141, 'name': '过流Ⅱ段经方向', 'value': 0}, {'id': 151, 'name': '过流Ⅲ段经方向', 'value': 0}, {'id': 161, 'name': '过流Ⅳ段经方向', 'value': 0}, {'id': 171, 'name': '过流Ⅰ段经复压', 'value': 0}, {'id': 181, 'name': '过流Ⅱ段经复压', 'value': 0}, {'id': 191, 'name': '过流Ⅲ段经复压', 'value': 0}, {'id': 201, 'name': '过流Ⅳ段经复压', 'value': 0}, {'id': 211, 'name': 'TV断线留过流Ⅰ段', 'value': 1}, {'id': 221, 'name': 'TV断线留过流其它段', 'value': 1}, {'id': 231, 'name': '重合闸检同期', 'value': 0}, {'id': 241, 'name': '重合闸检线无压母有压', 'value': 0}, {'id': 251, 'name': '重合闸检线有压母无压', 'value': 0}, {'id': 261, 'name': '重合闸检线无压母无压', 'value': 0}, {'id': 271, 'name': 'Ⅲ段及以上闭锁重合闸', 'value': 1}, {'id': 281, 'name': '多相故障闭锁重合闸', 'value': 0}, {'id': 291, 'name': '停用重合闸', 'value': 0}, {'id': 301, 'name': 'PT断线闭锁重合闸', 'value': 0}, {'id': 311, 'name': '不对称相继速动', 'value': 0}, {'id': 321, 'name': 'TWJ启动重合闸', 'value': 1}, {'id': 331, 'name': '重合加速距离Ⅲ段', 'value': 0}, {'id': 341, 'name': '投冲击性负荷', 'value': 1}]
    const filteredArray = newArray.filter(item => {
      if (queryInputcodevalue === '1') {
        return item.value === 1;  // 如果queryInputcodevalue为'1'，则过滤出value为1的对象
      } else {
        return item.value === 0;  // 否则，过滤出value为0的对象
      }
    });
    this.dingzhirows = filteredArray
    console.log(filteredArray);
    this.xianlu = localStorage.getItem('xianlu')
    this.fuhe = localStorage.getItem('fuhe')
    // 发送查询请求
    var response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_maxload": {
            "terms": {
              "field": "maxload",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    var hits = response.data.aggregations.unique_maxload.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    var keys = hits.map(hit => hit.key);
    this.ywNames = keys
    console.log(keys)
    // ------------------------------------------------------------------------
    // data.push({"key":hits[0].key+"-"+hits2[0].key,"value":hits[0].doc_count})

    // ------------------------操作票推荐---------------------------------------




    // 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_sysunitname": {
            "terms": {
              "field": "sysunitname.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_sysunitname.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamessysunitname = keys
    console.log(keys)





// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_syscontent": {
            "terms": {
              "field": "syscontent.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_syscontent.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamessyscontent = keys
    console.log(keys)





// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_comunitname": {
            "terms": {
              "field": "comunitname.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_comunitname.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamescomunitname = keys
    console.log(keys)


// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_comcontent": {
            "terms": {
              "field": "comcontent.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_comcontent.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamescomcontent = keys
    console.log(keys)



    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 事故预案推荐 namedetail

    let selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    let queryString = localStorage.getItem('sqlwhere')
    let groupBy = 'namedetail,namenum'
    var sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString + "group by " +groupBy + " order by namenum asc"

    response = await axios.post(
      'http://43.136.220.136:9002/_sql?format=json',
      {
        "query": sqlgroup
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response);
// 处理响应数据
    var results = response.data.rows;  // 从SQL查询结果获取rows
// 将查询结果中的namedetail字段值取出来组成一个数组

    this.data = results.map((row, index) => ({
      id: index + 1,  // 从1开始递增的id
      // index: index + 1,  // 从1开始递增的index
      align: 'left',  // 每项的align属性
      description: row[0],  // 假设第一列是description
      index: row[1]  // 假设第二列需要作为额外信息包含
    }));



    // 操作票类
    let operatemission = localStorage.getItem('operatemission')

    selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    queryString = localStorage.getItem('sqlwhere')
    groupBy = 'sysordernum,syscontent'
    if(operatemission==="送电"){

      sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString +" and syscontent like '%送电%'" + "group by " +groupBy + " order by sysordernum asc"
    }else{
      sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString +" and syscontent like '%停电%'" + "group by " +groupBy + " order by sysordernum asc"

    }
//
//     response = await axios.post(
//       'http://43.136.220.136:9002/_sql?format=json',
//       {
//         "query": sqlgroup
//       },
//       {
//         headers: {
//           'Content-Type': 'application/json',
//           'Authorization': 'Basic ' + btoa('admin:1992A1573')
//         }
//       }
//     );
//     console.log(response);
// // 处理响应数据
//     results = response.data.rows;  // 从SQL查询结果获取rows
//
// // 将查询结果中的namedetail字段值取出来组成一个数组
//
//     this.fuhe =results





    selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    queryString = localStorage.getItem('sqlwhere')
    groupBy = 'max(maxload)'
    sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString

    response = await axios.post(
      'http://43.136.220.136:9002/_sql?format=json',
      {
        "query": sqlgroup
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response);
// 处理响应数据
    results = response.data.rows;  // 从SQL查询结果获取rows

// 将查询结果中的namedetail字段值取出来组成一个数组

    this.fuhe =results



    selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    queryString = localStorage.getItem('sqlwhere')
    groupBy = 'max(begindate)'
    sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString

    response = await axios.post(
      'http://43.136.220.136:9002/_sql?format=json',
      {
        "query": sqlgroup
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response);
// 处理响应数据
    results = response.data.rows;  // 从SQL查询结果获取rows
// 将查询结果中的namedetail字段值取出来组成一个数组

    this.shijian =results




    // 定值单
    selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    queryString = localStorage.getItem('sqlwhere')
    groupBy = 'codename,codevalue'
    sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString + "group by " +groupBy

    response = await axios.post(
      'http://43.136.220.136:9002/_sql?format=json',
      {
        "query": sqlgroup
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response);
// 处理响应数据
    var results = response.data.rows;  // 从SQL查询结果获取rows
// 将查询结果中的namedetail字段值取出来组成一个数组

    this.dingzhirows = results.map((row, index) => ({
      id: index + 1,  // 从1开始递增的id
      // index: index + 1,  // 从1开始递增的index
      align: 'left',  // 每项的align属性
      name: row[0],  // 假设第一列是description
      value: row[1]  // 假设第二列需要作为额外信息包含
    }));










    // 操作票
    operatemission = localStorage.getItem('operatemission')

    selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    queryString = localStorage.getItem('sqlwhere')
    groupBy = 'sysordernum,syscontent'

    if(operatemission==="送电"){

      sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString +" and syscontent like '%送电%'" + "group by " +groupBy + " order by sysordernum asc"
    }else{
      sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString +" and syscontent like '%停电%'" + "group by " +groupBy + " order by sysordernum asc"

    }


    // selectString = localStorage.getItem('sqlselect')  //.slice(0, -1)
    // queryString = localStorage.getItem('sqlwhere')
    // groupBy = 'sysordernum,syscontent'
    // var sqlgroup = "select "+ groupBy +" from sqlgen where " + queryString + "group by " +groupBy + " order by sysordernum asc"
    // alert(sqlgroup)
    response = await axios.post(
      'http://43.136.220.136:9002/_sql?format=json',
      {
        "query": sqlgroup
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response);
// 处理响应数据
    var results = response.data.rows;  // 从SQL查询结果获取rows
// 将查询结果中的namedetail字段值取出来组成一个数组

    this.caozuopiaorows = results.map((row, index) => ({
      id: index + 1,  // 从1开始递增的id
      // index: index + 1,  // 从1开始递增的index
      align: 'left',  // 每项的align属性
      name: row[0],  // 假设第一列是description
      value: row[1]  // 假设第二列需要作为额外信息包含
    }));



    // const namedetails = results.map(row => row[0]); // 假设namedetail是第一列
    // this.ywNamesnamedetail = namedetails;
    // console.log(namedetails);
    // alert(namedetails)
//
// // 发送查询请求
//     response = await axios.post(
//       'http://43.136.220.136:9002/sqlgen/_search',
//       {
//         "size": 0,
//         "aggs": {
//           "unique_namedetail": {
//             "terms": {
//               "field": "namedetail.keyword",
//               "size": 10000
//             }
//           }
//         }
//       },
//       {
//         headers: {
//           'Content-Type': 'application/json',
//           'Authorization': 'Basic ' + btoa('admin:1992A1573')
//         }
//       }
//     );
//     console.log(response)
//     // 处理响应数据
//     hits = response.data.aggregations.unique_namedetail.buckets;
//     // 将这里下边的所有json中的key取出来组成一个数组
//     keys = hits.map(hit => hit.key);
//     this.ywNamesnamedetail = keys
//     console.log(keys)

    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>




    // ---------------------------预案----------------------------------------


// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_comps": {
            "terms": {
              "field": "comps.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_comps.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamescomps = keys
    console.log(keys)



// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_namedetail": {
            "terms": {
              "field": "namedetail.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_namedetail.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamesnamedetail = keys

    this.populateTableData()
    console.log(keys)



// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_codename": {
            "terms": {
              "field": "codename.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_codename.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamescodename = keys
    console.log(keys)


// 发送查询请求
    response = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_codevalue": {
            "terms": {
              "field": "codevalue",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response)
    // 处理响应数据
    hits = response.data.aggregations.unique_codevalue.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    keys = hits.map(hit => hit.key);
    this.ywNamescodevalue = keys
    console.log(keys)





    // -------------------------第二个参数-----------------------------------------------

    // 发送查询请求
    const response2 = await axios.post(
      'http://43.136.220.136:9002/sqlgen/_search',
      {
        "size": 0,
        "aggs": {
          "unique_operatemission": {
            "terms": {
              "field": "operatemission.keyword",
              "size": 10000
            }
          }
        }
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa('admin:1992A1573')
        }
      }
    );
    console.log(response2)
    // 处理响应数据
    const hits2 = response2.data.aggregations.unique_operatemission.buckets;
    // 将这里下边的所有json中的key取出来组成一个数组
    const keys2 = hits2.map(hit => hit.key);
    this.ywNames2 = keys2
    console.log(keys2)





  }

};
</script>
<style>
td.text-center {
  text-align: left;
}
td.text-right {
  text-align: left;
}

.q-table th {
  text-align: left;
}
</style>
